package com.ningmengs789.live.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.net.response.ChatTaskListResponse;
import com.ningmengs789.live.server.widget.CircleImageView;
import com.ningmengs789.live.utils.CommonUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class TaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ChatTaskListResponse.ListBean> mList = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private Context context;

    public TaskAdapter(Context context){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new Vh(layoutInflater.inflate(R.layout.task_adapter_item, parent, false));
        }else {
            return new V(layoutInflater.inflate(R.layout.task_adapter_item2, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Vh) {
            ((Vh) holder).setData(mList.get(position));
        }else if (holder instanceof V){
            ((V) holder).setData(mList.get(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        //get_status 1:可以领取，0:不可以领取
        if (mList.get(position).getGet_status() == 1){
            return 0;//带接受
        }
        return 1;//无接受
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setCards(List<ChatTaskListResponse.ListBean> list) {
        if (list == null) {
            return;
        }
        mList = list;
    }

    private MyItemClickListener mItemClickListener;

    public interface MyItemClickListener {
        void onItemClickPP(View view, String userId, int position);
        void onItClick(View view, int position);
    }

    public void setOnItemClickListener(MyItemClickListener listener){
        this.mItemClickListener = listener;
    }


    class Vh extends RecyclerView.ViewHolder {

        private CircleImageView mIcon;
        private TextView mNicknameTv;
        private TextView mCostTv;
        private ImageView task_adapter_item_iv;

        private TextView task_describe;
        private TextView task_time;

        public Vh(View itemView) {
            super(itemView);
            mIcon =  itemView.findViewById(R.id.task_adapter_item_icon);
            mNicknameTv = itemView.findViewById(R.id.task_adapter_item_nickname_tv);
            mCostTv = itemView.findViewById(R.id.task_adapter_item_cost_tv);

            task_adapter_item_iv = itemView.findViewById(R.id.task_adapter_item_iv);

            task_describe = itemView.findViewById(R.id.task_adapter_item_describe_tv);
            task_time = itemView.findViewById(R.id.task_time);


            itemView.findViewById(R.id.task_accept).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItClick(v, getLayoutPosition());
                }
            });
        }

        void setData(final ChatTaskListResponse.ListBean listBean) {
            mNicknameTv.setText(listBean.getUser_nickname());
            mCostTv.setText(listBean.getPer_cost() + context.getString(R.string.ql_cost));

            if (listBean.getTask_type() == 3){
                task_adapter_item_iv.setImageResource(R.drawable.task_video_);
            }else {
                task_adapter_item_iv.setImageResource(R.drawable.task_voice_);
            }

            String avatar = listBean.getAvatar();
            Glide.with(itemView.getContext())
                    .load(CommonUtils.getUrl(avatar))
                    .into(mIcon);

            String sex = "男";
            int age = listBean.getAge();
            if (listBean.getSex() != 1){
                sex = "女";
            }
            String city_name = listBean.getCity_name();
            task_describe.setText(city_name + "|" + sex + "|" + age + "岁");

            task_time.setText(listBean.getPublish_time());

            mIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClickPP(v, String.valueOf(listBean.getUser_id()), getLayoutPosition());
                }
            });
        }
    }

    class V extends RecyclerView.ViewHolder {

        private CircleImageView mIcon;
        private TextView mNicknameTv;
        private TextView mCostTv;
        private ImageView task_adapter_item_iv;

        private TextView task_time;

        private LinearLayout mControls;

        public V(View itemView) {
            super(itemView);
            mIcon =  itemView.findViewById(R.id.task_adapter_item_icon);
            mNicknameTv = itemView.findViewById(R.id.task_adapter_item_nickname_tv);
            mCostTv = itemView.findViewById(R.id.task_adapter_item_cost_tv);

            task_adapter_item_iv = itemView.findViewById(R.id.task_adapter_item_iv);

            task_time = itemView.findViewById(R.id.task_time);

            mControls = itemView.findViewById(R.id.task_adapter_item_ll);
        }

        void setData(final ChatTaskListResponse.ListBean listBean) {
            mNicknameTv.setText(listBean.getUser_nickname());
            mCostTv.setText(listBean.getPer_cost() + context.getString(R.string.ql_cost));

            if (listBean.getTask_type() == 3){
                task_adapter_item_iv.setImageResource(R.drawable.task_video_);
            }else {
                task_adapter_item_iv.setImageResource(R.drawable.task_voice_);
            }

            String avatar = listBean.getAvatar();
            Glide.with(itemView.getContext())
                    .load(CommonUtils.getUrl(avatar))
                    .into(mIcon);

            task_time.setText(listBean.getPublish_time());

            mControls.removeAllViews();
            if (listBean.getUser_list().size() > 0) {
                for (int i = 0; i < listBean.getUser_list().size(); i++) {
                    View view_ = View.inflate(context, R.layout.task_item_, null);
                    CircleImageView icon = view_.findViewById(R.id.task_item_icon);
                    Button state = view_.findViewById(R.id.task_item_state);
                    Glide.with(context)
                            .load(CommonUtils.getUrl(listBean.getUser_list().get(i).getAvatar()))
                            .into(icon);
                    if (listBean.getUser_list().get(i).getOnline_status() == 0){
                        state.setVisibility(View.GONE);
                    }else {
                        state.setVisibility(View.VISIBLE);
                    }
                    mControls.addView(view_);

                    final String userId = String.valueOf(listBean.getUser_list().get(i).getId());
                    view_.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mItemClickListener.onItemClickPP(v, userId, getLayoutPosition());
                        }
                    });
                }
            }


            mIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClickPP(v, String.valueOf(listBean.getUser_id()), getLayoutPosition());
                }
            });
        }
    }


}
